package com.gxuwz.business.service.impl;

import java.util.Date;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.gxuwz.business.dao.impl.FeeDAO;
import com.gxuwz.business.entity.Fee;
import com.gxuwz.business.entity.Student;
import com.gxuwz.business.entity.SysUser;
import com.gxuwz.business.entity.Treat;
import com.gxuwz.business.service.IFeeService;
import com.gxuwz.business.service.IRecMedService;
import com.gxuwz.business.service.IRecipeService;
import com.gxuwz.business.service.IStandFeeService;
import com.gxuwz.util.Pager;

@Service("feeService")
public class FeeServiceImpl implements IFeeService {
	
	@Autowired
	private FeeDAO feeDAO;
	@Autowired
	private IRecipeService recipeService;
	@Autowired
	private IStandFeeService standFeeService;
	@Autowired
	private IRecMedService recMedService;
	
	private Fee fee = new Fee();
	
	@Override
	public void add(Student student, Treat treat, SysUser user) {
		
		Double standFee = 0.0;//治疗标准费用
		List<Double> standFeeList;//治疗标准费用集合
		Double medFee = 0.0;//药品费用
		List<String> recipeIdList;//处编集合
		List<Double> medFeeList;//药品费用集合
		
		standFeeList = standFeeService.getFeeByRegId(student.getRegId());
		for(Double s_fee: standFeeList){
			standFee += s_fee;
		}
		recipeIdList = recipeService.getByTreatId(treat.getTreatId());
		for(String recipeId : recipeIdList){
			medFeeList = recMedService.getFeeByRecId(recipeId);
			for(Double m_fee: medFeeList){
				medFee += m_fee;
			}
		}
		fee.setUserId(user.getUserId());
		fee.setUserName(user.getUserName());
		fee.setStuId(student.getStuId());
		fee.setStuName(student.getStuName());
		fee.setTreatId(treat.getTreatId());
		fee.setRegId(student.getRegId());
		fee.setMedFee(medFee);
		fee.setStandFee(standFee);
		fee.setExtraFee(0.0);
		fee.setFeeTotal(medFee+standFee);
		fee.setFeePut(0.0);
		fee.setFeeNeed(medFee+standFee);
		fee.setFeeDate(new Date());
		fee.setStatus(0);
		
		feeDAO.save(fee);
		
	}

	/**
	 * 通过挂号编号查找
	 */
	@Override
	public Fee findByRegId(String regId){
		return feeDAO.findByRegId(regId);
	}

	@Override
	public void update(Fee fee) {
		feeDAO.update(fee);
		
	}

	@Override
	public void delete(Fee Fee) {
		// TODO Auto-generated method stub
		
	}

	@Override
	public Fee findById(String feeId) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public List<Fee> getRightAll() {
		// TODO Auto-generated method stub
		return null;
	}
	/**
	 * 修改治疗费
	 */
	@Override
	public void updateStandFee(Fee fee) {
		feeDAO.updateStandFee(fee);
	}



	@Override
	public Pager<Fee> getByPager(Fee fee, Integer page, Integer limit) {
		return feeDAO.getByPager(fee, page, limit);
	}

}
